import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:thingskit/Pages/Navigation/NavigationPage.dart';
import 'package:thingskit/apiService/login.dart';
import '../../components/textFieldWidget.dart';
import '../Home/homePage.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final TextEditingController _usernameController = TextEditingController(text: 'migan');
  final TextEditingController _passwordController = TextEditingController(text: 'migan');
  final FocusNode _usernameFocusNode = FocusNode(); // 用户名输入框的FocusNode
  final FocusNode _passwordFocusNode = FocusNode(); // 密码输入框的FocusNode

  Future<void> _login() async {
    //移除输入框焦点
    FocusScope.of(context).unfocus();
    String username = _usernameController.text.toString();
    String password = _passwordController.text.toString();
    if (username.isEmpty || password.isEmpty) {
      Get.snackbar('错误', '账号或密码不能为空', duration: Duration(milliseconds:800));
      return;
    }
    try {
      EasyLoading.show(
        status: '正在登录...',
        maskType: EasyLoadingMaskType.black,
      );
      final data = await LoginApi().login(username, password);
      if (data != null) {
        Get.off(() => NavigationPage());
      } else {
        Get.snackbar('错误', '登录失败', duration: Duration(milliseconds:800)); // 登录失败时弹框提示
      }
      EasyLoading.dismiss();
    } catch (e) {
      EasyLoading.dismiss();
      Get.snackbar('未知错误', '请检查网络连接或联系管理员', duration: Duration(milliseconds:800)); // 捕获异常并提示
    }
  }

  @override
  void dispose() {
    _usernameController.dispose();
    _passwordController.dispose();
    _usernameFocusNode.dispose();
    _passwordFocusNode.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text(''),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(32.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Text(
                '星闪智能灯',
                textAlign: TextAlign.start,
                style: TextStyle(
                  fontSize: 58,
                  fontWeight: FontWeight.bold,
                  color: Colors.blue,
                ),
              ),
              SizedBox(height: 36),
              Text(
                '欢迎使用\n专业的智能控制工具',
                textAlign: TextAlign.start,
                style: TextStyle(
                  fontSize: 24,
                  color: Colors.black,
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(height: 60),
              TextFieldWidget(controller: _usernameController,focusNode: _usernameFocusNode, label: '账号'),
              SizedBox(height: 20),
              TextFieldWidget(
                controller: _passwordController,
                focusNode: _passwordFocusNode,
                label: '密码',
                obscureText: true,
              ),
              SizedBox(height: 40),
              ElevatedButton(
                onPressed: _login,
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.blue,
                  padding: EdgeInsets.symmetric(vertical: 10),
                  textStyle: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                  foregroundColor: Colors.white,
                ),
                child: Text('登录'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
